package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.domain.SysReservation;
import com.zhentao.domain.UseBattery;
import com.zhentao.domain.vo.Result;
import com.zhentao.service.SysReservationService;
import com.zhentao.service.UseBatteryService;
import com.zhentao.mapper.UseBatteryMapper;
import com.zhentao.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 86133
* @description 针对表【use_battery】的数据库操作Service实现
* @createDate 2024-09-04 20:58:02
*/
@Service
public class UseBatteryServiceImpl extends ServiceImpl<UseBatteryMapper, UseBattery>
    implements UseBatteryService{
    @Autowired
    private UserUtils userUtils;
    @Autowired
    private SysReservationService sysReservationService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public Result addUseBattery(String uuid) {
        int i = userUtils.GetUserId();
        SysReservation one = sysReservationService.getOne(Wrappers.<SysReservation>lambdaQuery().eq(SysReservation::getUid, i));
        if (one == null){
            return Result.build(null,500,"请先预约");
        }
        UseBattery useBattery = new UseBattery();
        useBattery.setBid(one.getBid());
        useBattery.setUid(i);
        useBattery.setSid(one.getSid());
        useBattery.setCreateTime(new Date());
        boolean save = this.save(useBattery);
        sysReservationService.removeById(one.getId());
        if (uuid != null && !uuid.equals("")){
            stringRedisTemplate.delete(uuid);
        }
        return save?Result.build(null,200,"成功"):Result.build(null,500,"失败");
    }
}




